package initialize

import (
	_ "github.com/Sir-yuChen/funAnalyse/docs"
	"github.com/Sir-yuChen/funAnalyse/global"
	"github.com/Sir-yuChen/funAnalyse/middleware"
	"github.com/Sir-yuChen/funAnalyse/router"
	"github.com/gin-gonic/gin"
	/*
		go init
		windows下没有bin目录，首先我们需要找到swag的下载位置*…/github.com\swaggo\swag@v1.5.1\cmd\swag*
		在该目录下执行go install 会生成swag.exe到gopath的主目录下。
	*/
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

// Routers 初始化总路由
func Routers() *gin.Engine {
	Router := gin.Default()
	r := router.RouterGroupApp

	// 跨域，如需跨域可以打开下面的注释
	Router.Use(middleware.Cors()) // 直接放行全部跨域请求
	//Router.Use(middleware.CorsByRules()) // 按照配置的规则放行跨域请求
	Router.Use(middleware.CatchError()) //全局异常处理
	//接口文档
	Router.GET(global.API_SWAGGER.ApiPath, middleware.CheckApiResourcesMiddleware(global.API_SWAGGER), ginSwagger.WrapHandler(swaggerFiles.Handler))
	global.GVA_LOG.Info("register swagger handler")
	// 方便统一添加路由组前缀 多服务器上线使用 此处进行统一中间件等初始化操作
	PublicGroup := Router.Group(global.GVA_CONFIG.System.ApplicationName)
	{
		// 健康监测 系统启动默认加载部分数据
		PublicGroup.GET(global.API_HEALTH.ApiPath,
			middleware.CheckApiResourcesMiddleware(global.API_HEALTH),
			middleware.InitTplDBMiddleware(),
			func(c *gin.Context) { c.JSON(200, "ok") })
	}
	{
		r.Hello.HelloGetRouter(PublicGroup)
		r.BasisRouter.UserInfoRouter(PublicGroup)
		r.BasisRouter.CommonApiRouter(PublicGroup)
		r.BasisRouter.StockFilesApiRouter(PublicGroup)
		r.CasbinRouter.StockCasbinRoleApiRouter(PublicGroup)
		r.CasbinRouter.StockCasbinRoleRelationResourcesApiRouter(PublicGroup)
		r.CasbinRouter.StockCasbinRoleRelationUserApiRouter(PublicGroup)
		r.CasbinRouter.StockDeptApiRouter(PublicGroup)
		r.BasisRouter.StockMenuApiRouter(PublicGroup)
		r.SysRouter.SystemMoniterApiRouter(PublicGroup)
		r.NewsletterRouter.StockBulletinApiRouter(PublicGroup)
		r.NewsletterRouter.StockBulletinGroupApiRouter(PublicGroup)
		r.NewsletterRouter.CommunicationApiRouter(PublicGroup) //通讯
	}
	global.GVA_LOG.Info("router register success")
	return Router
}
